package com.sesum;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Sesum {

	private static final int REGISTER_FAILED = 1;

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {

		case REGISTER_FAILED:
			return new AlertDialog.Builder(this)

			.setTitle("Register Failed!")

			.setMessage("Sorry")

			.setNeutralButton("Aceptar",

			new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog,

				int which) {

				}

			}).create();

		default:
			break;
		}
		return null;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		// Get the EditText and Button References
		final EditText etUsername = (EditText) findViewById(R.id.username);
		final EditText etPassword = (EditText) findViewById(R.id.password);
		final EditText etPassword2 = (EditText) findViewById(R.id.password2);
		Button btnRegister = (Button) findViewById(R.id.register_button);
		Button btnCancel = (Button) findViewById(R.id.cancel_button);
		// lblResult = (TextView)findViewById(R.id.result);

		btnRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Check Login
				if (!etPassword.getText().equals(etPassword2.getText())) {
					Toast
							.makeText(getApplicationContext(),
									"Las contraseñas no son iguales",
									Toast.LENGTH_LONG);
				} else {
					boolean result = signup(etUsername.getText().toString(),
							etPassword.getText().toString(), "", "");
					if (result) {
						Intent intent = new Intent(getApplicationContext(),
								Activate.class);
						startActivity(intent);
					} else {
						showDialog(REGISTER_FAILED);
					}
				}
			}
		});

		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();

			}
		});

	}
}
